<?php

class mainHelper 
{
	/**
	 * 
	 * ONLY IN JOOMLA 1.6 ...
	 * @param string $path
	 * @param string $newName
	 * @param array $allow - Array extension to upload
	 * @param array $resize - array( 'w' => 100, 'h' => 100 )
	 * 
	 * @return file name
	 */
	function uploadFile($field = null, $path = null, $allow = array(), $resize = null)
	{
		$file = JRequest::get('files');
		
		jimport('joomla.filesystem.file');
		
		$filename = '';
		
		foreach ($file['jform'] as $key => $f)
		{
			if($file['jform']['name'][$field])
			{
				$tmp = array(
								'name' 		=> $file['jform']['name'][$field],
								'type' 		=> $file['jform']['type'][$field],
								'tmp_name' 	=> $file['jform']['tmp_name'][$field],
								'error' 	=> $file['jform']['error'][$field],
								'size' 		=> $file['jform']['size'][$field],
						);
						
				$extension = pathinfo($tmp['name'], PATHINFO_EXTENSION);
				
				if( isset($allow) && !in_array($extension, $allow) )
				{
					JError::raiseWarning(21, JText::_('Not allow file type.'));
					return false;
				}
				
				$filename = JFile::makeSafe($tmp['name']);
				
				$src = $tmp['tmp_name'];				
				$dest =  JPATH_ROOT.$path.$filename;
				
				$result = JFile::upload($src, $dest);
				
				if(!empty($resize))
				{
					require_once 'phpthumb/ThumbLib.inc.php';
					
					$options = array('resizeUp' => true, 'jpegQuality' => 80);

					$thumb = PhpThumbFactory::create($dest, $options);
					$thumb->resize( $resize['w'], $resize['h'] );
					//$thumb->show();
					$thumb->save($dest);
				}
				
				if(!$result)
				{
					return false;
				}
				
				break;
			}
		}
		
		return $filename;
	}
	
	/**
	 * 
	 * Function to get all info from youtube video
	 * @param string $video
	 * @param bool $embedCode, if it was set true, then $video will be embed code, else, it will be youtube video id
	 * 
	 * @return $info - array info about youtube video
	 */
	function getYoutubeVideoInfo($video = null, $embedCode = false)
	{
		$info = array();
		
		if($embedCode)
		{
			$pattern = '/http:\/\/www.youtube.com\/v\/([a-zA-z0-9\-_]+)/';			
			preg_match($pattern, $video, $matches, PREG_OFFSET_CAPTURE);
			
			$videoId = $matches[1][0];
		}
		else
			$videoId = $video;
		
		// set video data feed URL
	    $feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $videoId;
	
	    // read feed into SimpleXML object
	    $entry = simplexml_load_file($feedURL);
	    
	    // parse video entry
	    $videoInfo = self::parseVideoEntry($entry);
	    
	    return $videoInfo;
		
	}
	
	function parseVideoEntry($entry) 
	{
		$obj = new stdClass ();
		
		// get nodes in media: namespace for media information
		$media = $entry->children ( 'http://search.yahoo.com/mrss/' );
		$obj->title = $media->group->title;
		$obj->description = $media->group->description;
		
		// get video player URL
		$attrs = $media->group->player->attributes ();
		$obj->watchURL = $attrs ['url'];
		
		// get video thumbnail
		$attrs = $media->group->thumbnail [0]->attributes ();
		$obj->thumbnailURL = $attrs ['url'];
		
		// get <yt:duration> node for video length
		$yt = $media->children ( 'http://gdata.youtube.com/schemas/2007' );
		$attrs = $yt->duration->attributes ();
		$obj->length = $attrs ['seconds'];
		
		// get <yt:stats> node for viewer statistics
		$yt = $entry->children ( 'http://gdata.youtube.com/schemas/2007' );
		$attrs = $yt->statistics->attributes ();
		$obj->viewCount = $attrs ['viewCount'];
		
		// get <gd:rating> node for video ratings
		$gd = $entry->children ( 'http://schemas.google.com/g/2005' );
		if ($gd->rating) {
			$attrs = $gd->rating->attributes ();
			$obj->rating = $attrs ['average'];
		} else {
			$obj->rating = 0;
		}
		
		// get <gd:comments> node for video comments
		$gd = $entry->children ( 'http://schemas.google.com/g/2005' );
		if ($gd->comments->feedLink) {
			$attrs = $gd->comments->feedLink->attributes ();
			$obj->commentsURL = $attrs ['href'];
			$obj->commentsCount = $attrs ['countHint'];
		}
		
		// get feed URL for video responses
		$entry->registerXPathNamespace ( 'feed', 'http://www.w3.org/2005/Atom' );
		$nodeset = $entry->xpath ( "feed:link[@rel='http://gdata.youtube.com/schemas/2007#video.responses']" );
		if (count ( $nodeset ) > 0) {
			$obj->responsesURL = $nodeset [0] ['href'];
		}
		
		// get feed URL for related videos
		$entry->registerXPathNamespace ( 'feed', 'http://www.w3.org/2005/Atom' );
		$nodeset = $entry->xpath ( "feed:link[@rel='http://gdata.youtube.com/schemas/2007#video.related']" );
		if (count ( $nodeset ) > 0) {
			$obj->relatedURL = $nodeset [0] ['href'];
		}
		
		// return object to caller  
		return $obj;
	}
}